package com.yanqu.road.server.manager.strongholdwar;

import com.yanqu.road.entity.activity.ActivityConditionInfo;
import com.yanqu.road.entity.activity.ActivityInfo;
import com.yanqu.road.entity.activity.huashigang.HuaShiGangFloorConfig;
import com.yanqu.road.entity.activity.stronghold.config.*;
import com.yanqu.road.entity.config.goods.GoodsInfo;
import com.yanqu.road.entity.config.goods.MallInfo;
import com.yanqu.road.entity.enums.activity.stronghold.eStrongholdParamType;
import com.yanqu.road.entity.enums.activity.stronghold.eStrongholdTimeType;
import com.yanqu.road.entity.enums.eActivityType;
import com.yanqu.road.entity.servercenter.ServerInfo;
import com.yanqu.road.entity.union.activitygroup.UnionActivityGroup;
import com.yanqu.road.logic.bussiness.activity.StrongholdActivityBusiness;
import com.yanqu.road.logic.bussiness.servercenter.ActivityBussiness;
import com.yanqu.road.server.CrossBaseServer;
import com.yanqu.road.server.TempMgr;
import com.yanqu.road.server.manager.union.activitygroup.Cross2UnionActivityGroupMgr;
import com.yanqu.road.server.manger.ServerListMgr;
import com.yanqu.road.server.manger.TempCommonMgr;
import com.yanqu.road.server.manger.TempGoodsBagMgr;
import com.yanqu.road.utils.RandomHelper;
import com.yanqu.road.utils.date.DateHelper;

import java.math.BigDecimal;
import java.math.BigInteger;
import java.util.*;
import java.util.concurrent.ConcurrentHashMap;

public class CrossStrongholdActivityMgr extends TempMgr {
    public static final int CONDITION_TYPE_WILD_USER = 87;
    public static final int CONDITION_TYPE_WILD_UNION = 88;
    public static final int CONDITION_TYPE_ELIMINATE_USER = 89;
    public static final int CONDITION_TYPE_ELIMINATE_UNION = 90;
    public static final int CONDITION_TYPE_SILVER_DONAGE = 95;

    private static Map<Integer, ActivityInfo> activityInfoMap = new ConcurrentHashMap<>();
    private static Map<Integer, List<ActivityConditionInfo>> activityConditionInfoMap = new ConcurrentHashMap<>();
    private static Map<Integer, Map<Integer, List<ActivityConditionInfo>>> activityConditionInfoTypeMap = new ConcurrentHashMap<>();

    private static Map<Integer, Map<Integer, StrongholdWildConfig>> activityWildConfigMap = new ConcurrentHashMap<>();

    private static Map<Integer, Map<Integer, MallInfo>> mallInfoMap = new ConcurrentHashMap<>();
    private static Map<Integer, Map<Integer, HuaShiGangFloorConfig>> desertConfigMap = new ConcurrentHashMap<>();
    private static Map<Integer, Map<Integer, StrongholdTimeConfig>> timeConfigMap = new ConcurrentHashMap<>();
    private static Map<Integer, Map<Integer, StrongholdDonateConfig>> donateConfigMap = new ConcurrentHashMap<>();
    private static Map<Integer, Map<Integer, StrongholdTradeConfig>> tradeConfigMap = new ConcurrentHashMap<>();
    private static Map<Integer, Map<String, String>> paramConfigMap = new ConcurrentHashMap<>();
    private static Map<Integer, Map<Integer, StrongholdNpcConfig>> npcConfigMap = new ConcurrentHashMap<>();

    private static RandomHelper randomHelper = new RandomHelper();


    public static ActivityInfo getOpenActivityInfo(int activityId) {
        return activityInfoMap.get(activityId);
    }

    public static List<StrongholdWildConfig> getAllHouseInfo(int activotyId) {
        Map<Integer, StrongholdWildConfig> configMap = activityWildConfigMap.get(activotyId);
        if (configMap == null) {
            return new ArrayList<>();
        }
        return new ArrayList<>(configMap.values());
    }

    public static int getCurEmilinateRound(int actiivtyId) {
        int hours = getCurTime(actiivtyId);
        Map<Integer, StrongholdTimeConfig> timeConfigMap = CrossStrongholdActivityMgr.timeConfigMap.get(actiivtyId);
        for (StrongholdTimeConfig timeConfig : timeConfigMap.values()) {
            if (timeConfig.getBeginTime() <= hours && timeConfig.getEndTime() > hours) {
                if (timeConfig.getType() == eStrongholdTimeType.ELIMINATE.getValue()) {
                    return timeConfig.getTimeId();

                } else if (timeConfig.getType() == eStrongholdTimeType.REST.getValue()) {
                    return timeConfig.getTimeId() + 1;
                }
            }
        }
        return 0;
    }

    public static boolean isEliminateTime(int activityId) {
        return isInSpecialTime(activityId, eStrongholdTimeType.ELIMINATE);
    }

    public static boolean isRestTime(int activityId) {
        return isInSpecialTime(activityId, eStrongholdTimeType.REST);
    }

    private static boolean isInSpecialTime(int activityId, eStrongholdTimeType strongholdTimeType) {
        Map<Integer, StrongholdTimeConfig> configMap = timeConfigMap.get(activityId);
        if (configMap == null) {
            return false;
        }
        List<StrongholdTimeConfig> configList = getStrongholdTimeConfig(activityId, configMap);
        for (StrongholdTimeConfig config : configList) {
            if (config.getType() == strongholdTimeType.getValue()) {
                return true;
            }
        }
        return false;
    }

    private static List<StrongholdTimeConfig> getStrongholdTimeConfig(int activityId, Map<Integer, StrongholdTimeConfig> configMap) {
        List<StrongholdTimeConfig> configList = new ArrayList<>();
        long hours = getCurTime(activityId);
        for (StrongholdTimeConfig timeConfig : configMap.values()) {
            if (timeConfig.getBeginTime() <= hours && timeConfig.getEndTime() > hours) {
                configList.add(timeConfig);
            }
        }
        return configList;
    }


    public static Map<Integer, ActivityInfo> getOpenActivityInfoMap() {
        Map<Integer, ActivityInfo> dataMap = new ConcurrentHashMap<>();
        for (ActivityInfo activityInfo : activityInfoMap.values()) {
            if (activityInShowTime(activityInfo)) {
                dataMap.put(activityInfo.getActivityId(), activityInfo);
            }
        }
        return dataMap;
    }

    private static boolean activityInShowTime(ActivityInfo activityInfo) {
        if (null == activityInfo) {
            return false;
        }
        long nowTime = System.currentTimeMillis() / 1000;
        return nowTime >= activityInfo.getBeginShowTime() && nowTime < activityInfo.getEndShowTime();
    }

    public static List<ActivityConditionInfo> getActivityConditionInfoList(int activityId) {
        return activityConditionInfoMap.get(activityId);
    }

    public static ActivityConditionInfo getActivityConditionInfo(int activityId, int conditionId) {
        List<ActivityConditionInfo> conditionInfoList = activityConditionInfoMap.get(activityId);
        if (conditionInfoList == null) {
            return null;
        }
        for (ActivityConditionInfo info : conditionInfoList) {
            if (info.getConditionId() == conditionId) {
                return info;
            }
        }
        return null;
    }

    public static int maxBoxNum(int activityId) {
        Map<String, String> paramMap = paramConfigMap.get(activityId);
        String s = paramMap.get(eStrongholdParamType.WILD_BOX_MAX_NUM.getKey());
        if (s == null) {
            return 0;
        }
        return Integer.parseInt(s);
    }

    public static List<Integer> getBoxRefreshTime(int activityId) {
        Map<String, String> map = paramConfigMap.get(activityId);
        List<Integer> refreshTimeList = new ArrayList<>();
        if (map == null) {
            return refreshTimeList;
        }
        String time = map.get("box_refresh_time");
        if (time == null) {
            return refreshTimeList;
        }
        String[] arr = time.split(";");
        for (String s : arr) {
            refreshTimeList.add(Integer.parseInt(s));
        }
        return refreshTimeList;
    }

    public static List<StrongholdNpcConfig> getAllNpcConfig(int activityId) {
        return new ArrayList<>(npcConfigMap.get(activityId).values());
    }

    public static StrongholdNpcConfig getNpcConfig(int activityId, int npcId) {
        return npcConfigMap.get(activityId).get(npcId);
    }

    public static StrongholdWildConfig getHouseInfo(int activityId, int wildId) {
        return activityWildConfigMap.get(activityId).get(wildId);
    }

    public static int getHouseItem(int activityId, int houseType) {
        Map<String, String> map = paramConfigMap.get(activityId);
        String s = map.get(eStrongholdParamType.WILD_ITEM_ID_PARAM.getKey());
        return Integer.parseInt(s.split(";")[houseType - 1]);
    }

    public static boolean canOpenBox(int activityId, int boxType, long pickUpTime) {
        GoodsInfo goodsInfo = TempCommonMgr.getGoodsInfo(getBoxItemId(activityId, boxType));
        if (goodsInfo == null) {
            return false;
        }
        List<BigInteger> paramList = goodsInfo.getParamList();
        if (paramList.isEmpty()) {
            return false;
        }
        int cdTime = paramList.get(0).intValue();
        return System.currentTimeMillis() - pickUpTime * 1000 >= cdTime * 1000;
    }

    public static int getInitRewardPoint(int activityId) {
        Map<String, String> stringMap = paramConfigMap.get(activityId);
        if (stringMap == null) {
            return 0;
        }
        String s = stringMap.get(eStrongholdParamType.WILD_REWARD_POINT_PARAM.getKey());
        if (s == null) {
            return 0;
        }
        String[] split = s.split(";");
        return Integer.parseInt(split[0]);
    }

    public static int getRewardPointsId(int activityId) {
        Map<String, String> stringMap = paramConfigMap.get(activityId);
        try {
            String s = stringMap.get("wild_integral_item");
            return Integer.parseInt(s);
        } catch (Exception e) {
            getLogger().error("wild_integral_item error. {}", stringMap.get("wild_integral_item"), e);
        }
        return 0;
    }

    public static int calcCurBlood(BigInteger curBlood, BigInteger totalBlood) {
        if (curBlood.compareTo(BigInteger.ZERO) <= 0) {
            return 0;
        }
        if (curBlood.equals(totalBlood)) {
            return 100;
        }
        int blood = new BigDecimal(curBlood).multiply(BigDecimal.valueOf(100)).divide(new BigDecimal(totalBlood), BigDecimal.ROUND_UP).intValue();
        if (blood == 100) {
            blood = 99;
        }
        return blood;
    }


    public static int calcEliminateCurBlood(BigInteger curBlood, BigInteger totalBlood) {
        if (curBlood.compareTo(BigInteger.ZERO) <= 0) {
            return 0;
        }
        if (curBlood.equals(totalBlood)) {
            return 10000;
        }
        int blood = new BigDecimal(curBlood).multiply(BigDecimal.valueOf(10000)).divide(new BigDecimal(totalBlood), BigDecimal.ROUND_UP).intValue();
        if (blood == 10000) {
            blood = 9999;
        }
        return blood;
    }


    public static int getCurTime(int activityId) {
        ActivityInfo activityInfo = activityInfoMap.get(activityId);
        if (activityInfo == null) {
            return 0;
        }
        long beginTime = activityInfo.getBeginTime();
        long nowTime = System.currentTimeMillis() / 1000 - beginTime;
        int curTime = (int) nowTime / 3600;
        return curTime;
    }

    public static int getBoxRewardPoints(int activityId, int type) {
        int itemId = getBoxItemId(activityId, type);
        GoodsInfo goodsInfo = TempCommonMgr.getGoodsInfo(itemId);
        if (goodsInfo == null) {
            return 0;
        }
        return goodsInfo.getParamList().get(1).intValue();
    }

    public static int getBoxCdTime(int activityId, int type) {
        GoodsInfo goodsInfo = TempCommonMgr.getGoodsInfo(getBoxItemId(activityId, type));
        return goodsInfo.getParamList().get(0).intValue();
    }

    public static int getKillPoints(int activityId) {
        Map<String, String> stringMap = paramConfigMap.get(activityId);
        if (stringMap == null) {
            return 0;
        }
        String s = stringMap.get(eStrongholdParamType.WILD_REWARD_POINT_PARAM.getKey());
        if (s == null) {
            return 0;
        }
        String[] split = s.split(";");
        return Integer.parseInt(split[1]);
    }

    public static String getDenfendPointsParam(int activityId) {
        Map<String, String> stringMap = paramConfigMap.get(activityId);
        return stringMap.get(eStrongholdParamType.WILD_DEFEND_REWAD_POINT_PARAM.getKey());
    }

    /**
     * 当前轮次
     *
     * @param activityId
     * @return
     */
    public static int getUploadEliminateRound(int activityId) {
        int hours = getCurTime(activityId);
        Map<Integer, StrongholdTimeConfig> timeConfigMap = CrossStrongholdActivityMgr.timeConfigMap.get(activityId);
        for (StrongholdTimeConfig timeConfig : timeConfigMap.values()) {
            if (timeConfig.getBeginTime() <= hours && timeConfig.getEndTime() > hours && timeConfig.getType() == eStrongholdTimeType.REST.getValue()) {
                return timeConfig.getTimeId() + 1;
            }
        }
        return 0;
    }

    public static String[] getSocreParam(int activityId) {
        Map<String, String> map = paramConfigMap.get(activityId);
        return map.get(eStrongholdParamType.UNION_INTEGRAL_PARAM.getKey()).split(";");
    }

    /**
     * 联盟积分继承参数
     *
     * @param activityId
     * @return
     */
    public static String getUnionScoreInherit(int activityId) {
        Map<String, String> map = paramConfigMap.get(activityId);
        return map.get(eStrongholdParamType.UNION_INTEGRAL_INHERIT.getKey());
    }

    public static List<Integer> getBoxIdList(int activityId) {
        List<Integer> list = new ArrayList<>();
        Map<String, String> stringMap = paramConfigMap.get(activityId);
        if (stringMap == null) {
            return list;
        }
        try {
            String[] strings = stringMap.get(eStrongholdParamType.WILD_BOX_ID_PARAM.getKey()).split(";");
            for (String s : strings) {
                list.add(Integer.parseInt(s));
            }
        } catch (Exception e) {


        }
        return list;
    }

    public static String getWildName(int activityId, int wildId) {
        Map<Integer, StrongholdWildConfig> wildConfigMap = activityWildConfigMap.get(activityId);
        if (wildConfigMap == null) {
            return String.valueOf(wildId);
        }
        StrongholdWildConfig config = wildConfigMap.get(wildId);
        if (config == null) {
            return String.valueOf(wildId);
        }
        return config.getName();
    }

    public static int getBoxItemId(int activityId, int type) {
        try {
            Map<String, String> stringMap = paramConfigMap.get(activityId);
            String[] strings = stringMap.get(eStrongholdParamType.WILD_BOX_ID_PARAM.getKey()).split(";");
            return Integer.parseInt(strings[type]);
        } catch (Exception e) {


        }
        return 0;
    }

    public static int getRandomId(int activityId, List<Integer> idList) {
        int num = getQualifyNum(activityId);
        List<Integer> tempList = new ArrayList<>();
        for (int i = 1; i <= num; i++) {
            if (idList.contains(i)) {
                continue;
            }
            tempList.add(i);
        }
        if (tempList.size() <= 0) {
            return 0;
        }
        int index = randomHelper.next(0, tempList.size());
        return tempList.get(index);
    }

    public static Collection<Integer> getEliminateRoundList(int activtyId) {
        TreeSet<Integer> set = new TreeSet<>();
        Map<Integer, StrongholdTimeConfig> configMap = timeConfigMap.get(activtyId);
        for (StrongholdTimeConfig config : configMap.values()) {
            if (config.getType() == eStrongholdTimeType.ELIMINATE.getValue()) {
                set.add(config.getTimeId());
            }
        }
        return set;
    }

    public static int getLosePoints(int activityId) {
        try {
            Map<String, String> stringMap = paramConfigMap.get(activityId);
            return Integer.parseInt(stringMap.get("wild_lose_reward_integral"));
        } catch (Exception e) {

        }
        return 0;
    }

    public static int getWildWinReward(int activityId) {
        try {
            Map<String, String> stringMap = paramConfigMap.get(activityId);
            return Integer.parseInt(stringMap.get("wild_attack_reward_item").split("\\|")[1]);
        } catch (Exception e) {

        }
        return 0;
    }

    public static int getWildLoseReward(int activityId) {
        try {
            Map<String, String> stringMap = paramConfigMap.get(activityId);
            return Integer.parseInt(stringMap.get("wild_attack_reward_item").split("\\|")[0]);
        } catch (Exception e) {

        }
        return 0;
    }

    public static int getMammonHelpItemCd(int activityId){
        try {
            Map<String, String> stringMap = paramConfigMap.get(activityId);
            return Integer.parseInt(stringMap.get("ITEM_TIME_INTERVAL"));
        } catch (Exception e) {
            getLogger().error("据点战财神助力物品cd ITEM_TIME_INTERVAL 读取失败");
        }
        return 0;
    }

    public static boolean isLookWildRank(int activityId) {
        ActivityInfo activityInfo = activityInfoMap.get(activityId);
        long beginTime = activityInfo.getBeginTime();
        StrongholdTimeConfig timeConfig = getFirstRoundEliminateTimeConfig(activityId);
        //第一轮淘汰赛结束时间
        long endTime = beginTime + timeConfig.getEndTime() * 3600;
        return System.currentTimeMillis() < endTime * 1000;
    }

    private static StrongholdTimeConfig getFirstRoundEliminateTimeConfig(int activityId) {
        StrongholdTimeConfig config = null;
        for (StrongholdTimeConfig timeConfig : timeConfigMap.get(activityId).values()) {
            if (timeConfig.getType() == eStrongholdTimeType.ELIMINATE.getValue()) {
                //如果是淘汰赛，比较开始时间，取最小的，第一轮淘汰赛
                if (config == null || timeConfig.getBeginTime() < config.getBeginTime()) {
                    config = timeConfig;
                }
            }
        }
        return config;
    }

    /**
     * 每个联盟生成随机排序列表
     * @param activityId
     * @return
     */
    public static List<Integer> getRandomSortList(int activityId) {
        List<Integer> list = new ArrayList<>();
        int num = getQualifyNum(activityId);
        List<Integer> tempList = new ArrayList<>();
        for (int i = 1; i <= num; i++) {
            tempList.add(i);
        }
        while (!tempList.isEmpty()) {
            int index = randomHelper.next(0, tempList.size());
            Integer value = tempList.remove(index);
            list.add(value);
        }
        return list;
    }


    @Override
    public boolean reloadData() throws Exception {
        loadActivity();
        return true;
    }

    public static void loadActivity() {
        String nowTimeStr = DateHelper.getCurrentDateTimeString();
        List<Integer> activityTypeList = new ArrayList<>();
        activityTypeList.add(eActivityType.StrongholdWarActivity.getValue());
        Map<Integer, ActivityInfo> tempActivityInfoMap = ActivityBussiness.getOpenCrossRankActivityInfoMap(activityTypeList, nowTimeStr);
        List<Integer> activityIdList = new ArrayList<>();
        for (ActivityInfo activityInfo : tempActivityInfoMap.values()) {
            Map<Integer, UnionActivityGroup> activityGroupMap = Cross2UnionActivityGroupMgr.getGroupMap(activityInfo.getActivityId());
            if (null != activityGroupMap) {
                for (UnionActivityGroup activityGroup : activityGroupMap.values()) {
                    if (activityGroup.getCrossServerId() == CrossBaseServer.getInstance().getServerId()){
                        activityIdList.add(activityInfo.getActivityId());
                        break;
                    }
                }
            }
        }

        Map<Integer, List<ActivityConditionInfo>> tempActivityConditionInfoMap = new ConcurrentHashMap<>();
        Map<Integer, Map<Integer, List<ActivityConditionInfo>>> tempActivityConditionTypeInfoMap = new ConcurrentHashMap<>();
        ActivityBussiness.getActivityConditionInfoMap(tempActivityConditionTypeInfoMap, tempActivityConditionInfoMap, activityIdList);
        if (!activityIdList.isEmpty()) {
            activityInfoMap = tempActivityInfoMap;
        }
        activityConditionInfoMap = tempActivityConditionInfoMap;
        activityConditionInfoTypeMap = tempActivityConditionTypeInfoMap;
        mallInfoMap = ActivityBussiness.getActivityMallInfoMap(activityIdList);
        for (ActivityInfo activityInfo : activityInfoMap.values()) {
            loadNewActivity(activityInfo);
        }
    }

    private static void loadNewActivity(ActivityInfo activityInfo) {
        int activityId = activityInfo.getActivityId();

        Map<Integer, HuaShiGangFloorConfig> tempDesertConfigMap = StrongholdActivityBusiness.getStrongholdDesertConfig(activityId);
        Map<Integer, StrongholdTimeConfig> tempTimeConfigMap = StrongholdActivityBusiness.getStrongholdTimeConfig(activityId);
        Map<Integer, StrongholdWildConfig> tempWildConfigMap = StrongholdActivityBusiness.getStrongholdWildConfig(activityId);
        Map<Integer, StrongholdDonateConfig> tempDonateConfigMap = StrongholdActivityBusiness.getStrongholdDonateConfig(activityId);
        Map<Integer, StrongholdTradeConfig> tempTradeConfigMap = StrongholdActivityBusiness.getStrongholdTradeConfig(activityId);
        Map<String, String> tempPramConfigMap = StrongholdActivityBusiness.getStrongholdParamConfigMap(activityId);
        Map<Integer, StrongholdNpcConfig> tempNpcConfigMap = StrongholdActivityBusiness.getStrongholdNpcConfig(activityId);

        Map<Integer, List<ActivityConditionInfo>> tempActivityConditionInfoMap = new ConcurrentHashMap<>();
        Map<Integer, Map<Integer, List<ActivityConditionInfo>>> tempActivityConditionTypeInfoMap = new ConcurrentHashMap<>();
        ActivityBussiness.getActivityConditionInfoMap(tempActivityConditionTypeInfoMap, tempActivityConditionInfoMap, Collections.singletonList(activityId));
        for (List<ActivityConditionInfo> list : tempActivityConditionInfoMap.values()) {
            activityConditionInfoMap.put(activityId, list);
        }
        for (Map<Integer, List<ActivityConditionInfo>> listMap : tempActivityConditionTypeInfoMap.values()) {
            activityConditionInfoTypeMap.put(activityId, listMap);
        }
        desertConfigMap.put(activityId, tempDesertConfigMap);
        timeConfigMap.put(activityId, tempTimeConfigMap);
        donateConfigMap.put(activityId, tempDonateConfigMap);
        tradeConfigMap.put(activityId, tempTradeConfigMap);
        paramConfigMap.put(activityId, tempPramConfigMap);
        activityWildConfigMap.put(activityId, tempWildConfigMap);
        npcConfigMap.put(activityId, tempNpcConfigMap);
    }

    public static ActivityInfo reloadActivityInfo(int activityId) {
        ActivityInfo activityInfo = ActivityBussiness.getActivityInfoByActivityId(activityId);
        if (activityInShowTime(activityInfo)) {
            loadNewActivity(activityInfo);
            TempCommonMgr.reloadCommonData();
            TempGoodsBagMgr.reloadGoodsBagMap();
            activityInfoMap.put(activityId, activityInfo);
            return activityInfo;
        }
        return null;
    }

    public static int getQualifyNum(int activityId) {
        ActivityInfo activityInfo = activityInfoMap.get(activityId);
        if (activityInfo == null) {
            return 0;
        }
        long hours = getCurTime(activityId);
        int timeId = 0;
        Map<Integer, StrongholdTimeConfig> configMap = timeConfigMap.get(activityId);
        for (StrongholdTimeConfig timeConfig : configMap.values()) {
            if (timeConfig.getBeginTime() <= hours && timeConfig.getEndTime() > hours) {
                if (timeConfig.getType() == eStrongholdTimeType.REST.getValue()) {
                    timeId = timeConfig.getTimeId() - 1;
                } else if (timeConfig.getType() == eStrongholdTimeType.ELIMINATE.getValue()) {
                    timeId = timeConfig.getTimeId() - 2;
                }
                break;
            }
        }
        if (timeId == 0) {
            return 0;
        }
        return Integer.parseInt(configMap.get(timeId).getParam());
    }

    public static boolean isAfterWildTime(int activityId) {
        CrossStrongholdMgr.loadActivity(activityId);
        ActivityInfo activityInfo = activityInfoMap.get(activityId);
        if (null == activityInfo){
            return false;
        }
        long beginTime = activityInfo.getBeginTime();
        for (StrongholdTimeConfig timeConfig : timeConfigMap.get(activityId).values()) {
            if (timeConfig.getType() == eStrongholdTimeType.WILD.getValue()) {
                long wildEndTime = (beginTime + timeConfig.getEndTime() * 3600) * 1000;
                return System.currentTimeMillis() > wildEndTime;
            }
        }
        return true;
    }

    public static int getCurBaseLv(int activityId,int curLv, List<Integer> curProcess) {
        int lv = curLv;
        while (nextLv(activityId,lv, curProcess)) {
            lv++;
        }
        return lv;
    }

    private static boolean nextLv(int activityId,int curLv, List<Integer> curProcess) {
        Map<Integer, StrongholdDonateConfig> configMap = donateConfigMap.get(activityId);
        StrongholdDonateConfig nextConfig = configMap.get(curLv + 1);
        if (nextConfig == null) {
            return false;
        }
        StrongholdDonateConfig config = configMap.get(curLv);
        if (config == null) {
            return false;
        }
        String needProcess = config.getNeedProcess();
        String[] arr = needProcess.split("\\|");
        for (int i = 0; i < arr.length; i++) {
            int needValue = Integer.parseInt(arr[i]);
            Integer curValue = curProcess.get(i);
            if (curValue < needValue) {
                return false;
            }
        }
        return true;
    }

    public static int getUnionBaseAdd(int activityId,int lv) {
        Map<Integer, StrongholdDonateConfig> configMap = donateConfigMap.get(activityId);
        StrongholdDonateConfig config = configMap.get(lv);
        if (config != null) {
            return config.getHpAdd();
        }
        return 0;
    }


    public static int getHpAddByRank(int activityId,int rank) {
        if (rank <= 0) {
            return 0;
        }
        Map<String, String> map = paramConfigMap.get(activityId);
        String rankAdditionParam = map.get("union_blood_rank_addition");
        String[] arr = rankAdditionParam.split(";");
        if (rank > arr.length) {
            return 0;
        }
        return Integer.parseInt(arr[rank - 1]);
    }

    public static BigInteger calcBlood(int activityId, BigInteger ability) {
        String param = paramConfigMap.get(activityId).get("union_blood_param");
        String[] arr = param.split("\\|");
        Map<Integer, Integer> abilityEnlargeMap = new HashMap<>();
        for (String s : arr) {
            String[] split = s.split(";");
            if (split.length >= 2) {
                int key = Integer.parseInt(split[0]);
                int value = Integer.parseInt(split[1]);
                abilityEnlargeMap.put(key, value);
            } else if (split.length == 1) {
                //默认乘以1000
                abilityEnlargeMap.put(0, Integer.parseInt(split[0]) * 1000);
            }
        }
        int openDays = getOpenDays(activityId);
        int day = Integer.MAX_VALUE;
        int enlargeValue = 1;
        int maxDays = 0;
        for (Map.Entry<Integer, Integer> entry : abilityEnlargeMap.entrySet()) {
            Integer days = entry.getKey();
            if (days >= maxDays) {
                maxDays = days;
            }
            if (openDays < days && days < day) {
                //如果当前天数大于配置
                enlargeValue = entry.getValue();
                day = days;
            }
        }
        if (openDays >= maxDays) {
            enlargeValue =  abilityEnlargeMap.get(maxDays);
        }
        return new BigDecimal(ability).multiply(new BigDecimal(enlargeValue)).divide(new BigDecimal(1000), BigDecimal.ROUND_UP).toBigInteger();
    }

    private static int getOpenDays(int activityId) {
        //秒级
        ActivityInfo activityInfo = getOpenActivityInfo(activityId);
        long beginShowTime = activityInfo.getBeginShowTime();
        List<Long> serverIdList = activityInfo.getServerIdList();
        //秒级
        long minOpenTime = 0;
        for (Long serverId : serverIdList) {
            ServerInfo serverInfo = ServerListMgr.getServerInfo(serverId);
            if (serverInfo != null) {
                if (minOpenTime == 0) {
                    minOpenTime = serverInfo.getOpenTime();
                } else if (minOpenTime > serverInfo.getOpenTime()) {
                    minOpenTime = serverInfo.getOpenTime();
                }
            }
        }
        long days = (beginShowTime - minOpenTime) / 3600 / 24;
        return (int) days + 1;
    }

    @Override
    public boolean init() throws Exception {
        return reload();
    }

    @Override
    public boolean reloadConfig() throws Exception {
        return true;
    }

    @Override
    public boolean stop() {
        return true;
    }
}
